package com.bytedance.scenedemo.architecture_patterns.scope;

import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.utlity.ViewIdGenerator;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;

public class ScopeSample extends GroupScene {
    private Button btnClickScope;
    private Text txtCounterScopeNum;
    private int num = 0;
    private String str = "Counter %1$d";
    private int mId;

    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        ComponentContainer componentContainer = (ComponentContainer) inflater.parse(ResourceTable.Layout_scope_sample, container, false);
        initView(componentContainer);
        return componentContainer;
    }

    private void initView(ComponentContainer componentContainer) {
        btnClickScope = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_click_scope);
        Font font = new Font.Builder(btnClickScope.getText()).setWeight(Font.BOLD).build();
        btnClickScope.setFont(font);
        txtCounterScopeNum = (Text) componentContainer.findComponentById(ResourceTable.Id_txt_counter_scope_num);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setStroke(15, RgbColor.fromArgbInt(Color.getIntColor("#aed581")));
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#D6D7D7")));
        btnClickScope.setBackground(shapeElement);
        txtCounterScopeNum.setText(String.format(str, num));
    }

    @Override
    public void onActivityCreated(Intent intent) {
        super.onActivityCreated(intent);
        mId = ViewIdGenerator.generateViewId();
        StackLayout stackLayout = new StackLayout(requireSceneContext());
        stackLayout.setId(mId);
        btnClickScope.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                num++;
                txtCounterScopeNum.setText(String.format(str, num));
            }
        });
    }
}
